﻿@using Grand.Business.Core.Interfaces.Common.Security
@using Grand.Business.Core.Interfaces.System.Admin
@using Grand.Domain.Permissions
@using Grand.Web.Common.Menu
@using Constants = Grand.SharedUIResources.Constants
@inject IPermissionService permissionService
@inject IAdminSiteMapService adminSiteMapService
@inject IEnumerable<IAdminMenuProvider> _adminMenuProviders
@{
    //load sitemap
    var siteMap = new SiteMap(adminSiteMapService, permissionService);
    await siteMap.Load();
}
<div id="sidebar-button" class="sidebar_button" onclick="sidebarToggle()">
    <span></span>
    <span></span>
    <span></span>
</div>
<div class="col-md-3 left_col">
    <div class="left_col scroll-view">
        <div class="navbar nav_title">
            <a href="~/Admin" class="site_title">
                <img src="@(Constants.WwwRoot)/administration/build/images/grandLogo.png" width="35" height="35"/> <span>GrandNode</span>
            </a>
        </div>
        <div class="clearfix"></div>
        <div class="main_menu_side hidden-print main_menu" id="sidebar-menu">
            <div class="menu_section">
                <ul class="nav side-menu page-sidebar-menu">
                    @{
                        //standard (default) items
                        var rootNode = siteMap.RootNode;

                        //plugins
                        foreach (var adminMenuPlugin in _adminMenuProviders.ToList())
                        {
                            await adminMenuPlugin.ManageSiteMap(rootNode);
                        }

                        //"Plugins" menu item should be visible when it has some child nodes
                        var pluginNode = rootNode.ChildNodes.FirstOrDefault(x => x.SystemName == "Third party plugins");
                        if (pluginNode is { Visible: true })
                        {
                            var hasManagePluginsPermission = await permissionService.Authorize(StandardPermission.ManagePlugins);
                            pluginNode.Visible = hasManagePluginsPermission && pluginNode.ChildNodes.Any(x => x.Visible);
                        }
                    }
                    @foreach (var item in rootNode.ChildNodes.Where(x => x.Visible))
                    {
                        <partial name="Partials/MenuItem" model="item"/>
                    }
                </ul>
            </div>
        </div>
    </div>
</div>